package com.ujs.edu.demo.config;

import com.ujs.edu.demo.dataobject.BcAdmin;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class AuthHanderInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod){
            HandlerMethod handlerMethod=(HandlerMethod)handler;
            NeedLogin needLogin=handlerMethod.getMethodAnnotation(NeedLogin.class);
            if (needLogin!=null){
                BcAdmin bcUser=(BcAdmin) request.getSession().getAttribute("bcAdmin");
                if (bcUser==null){
                    response.sendRedirect("/tologin");
                    return false;
                }
            }
        }
        return true;
    }
}
